package host.haiwai.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * 这是一个 mybatis-plus代码生成器。
 * by：joefche / jc
 * date: 2021-8-28
 */
public class Auto {
    public static void main(String[] args) {
        //创建代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        //设置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setUrl("jdbc:mysql://192.168.31.236:3307/curd?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        autoGenerator.setDataSource(dataSourceConfig);

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOpen(false);
        globalConfig.setAuthor("joefche / JC");
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        globalConfig.setServiceName("%sServer"); //去掉 i 前缀
        globalConfig.setSwagger2(true);  //开启swagger2 文档接口
        globalConfig.setFileOverride(true); //是否覆盖之前生成的文件
        autoGenerator.setGlobalConfig(globalConfig);

        //包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("host.haiwai");
        autoGenerator.setPackageInfo(packageConfig);

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setEntityLombokModel(true); //设置 lombok 字段
        strategyConfig.setTablePrefix("jc_"); //设置表前缀
        //strategyConfig.setInclude("")  //这里是设置要生成的表名。不设置，默认全部
        //strategyConfig.setNaming(NamingStrategy.underline_to_camel); //表名，有表前缀的，不用驼峰
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); //字段驼峰法
        strategyConfig.setLogicDeleteFieldName("deleted"); //设置逻辑删除字段。还要在 application.yml 中配置一下
        strategyConfig.setVersionFieldName("version");  //设置乐观锁字段

        //自动填充时间
        List<TableFill> list = new ArrayList<>();
        TableFill createTime = new TableFill("add_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("edit_time", FieldFill.UPDATE);
        list.add(createTime);
        list.add(updateTime);
        strategyConfig.setTableFillList(list);

        autoGenerator.setStrategy(strategyConfig);


        autoGenerator.execute();

    }
}
